﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataObject;
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;

public partial class Upload : System.Web.UI.Page
{


    protected void Page_Load(object sender, EventArgs e)
    {
        pnlResult.Visible = false;
        dataBind();
    }
    protected void btUploadParts_Click(object sender, EventArgs e)
    {
        try
        {
            if (this.fuParts.HasFile)
            {
                uploadFile(new Parts(), fuParts);
                pnlResult.Visible = true;
                lbResult.Text = "Parts Descriptions submitted";
            }
        }
        catch (Exception ex)
        {
            pnlResult.Visible = true;
            lbResult.Text = "There is an error when you are submitting Parts Descriptions" + "\n" + ex.Message;
        }
    }


    protected void btUploadMarks_Click(object sender, EventArgs e)
    {
        try
        {
            if (this.fuMarks.HasFile)
            {
                uploadFile(new MarkDetails(), fuMarks);
                pnlResult.Visible = true;
                lbResult.Text = "MarkDetails submitted";
            }
        }
        catch (Exception ex)
        {
            pnlResult.Visible = true;
            lbResult.Text = "There is an error when you are submitting MarkDetails" + "\n" + ex.Message;
        }
    }


    protected void btDelete_Click(object sender, EventArgs e)
    {
        deleteTable("Answers");
        deleteTable("Questions");
        deleteTable("MarkDetails");
        deleteTable("Parts");
    }

    protected void btQuestions_Click(object sender, EventArgs e)
    {
        try
        {
            if (this.fuQuestions.HasFile)
            {
                uploadFile(new Questions(), fuQuestions);
                pnlResult.Visible = true;
                lbResult.Text = "Questions submitted";
            }
        }
        catch (Exception ex)
        {
            pnlResult.Visible = true;
            lbResult.Text = "There is an error when you are submitting Questions" + "\n" + ex.Message;
        }
    }

    private void dataBind()
    {
        string sql_script = @"SELECT * from Parts";
        string sql_script2 = @"select * from Questions";
        string sql_script3 = @"select * from MarkDetails";
        IDbConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["SurveyConnectionString"].ConnectionString);
        using (conn)
        {
            conn.Open();
            IDbCommand cmd = conn.CreateCommand();
            cmd.CommandText = sql_script;
            IDataReader dr = cmd.ExecuteReader();
            gvParts.DataSource = dr;
            gvParts.DataBind();
            dr.Close();

            cmd.CommandText = sql_script2;
            IDataReader dr2 = cmd.ExecuteReader();
            gvQuestions.DataSource = dr2;
            gvQuestions.DataBind();
            dr2.Close();

            cmd.CommandText = sql_script3;
            SqlDataReader dr3 = (SqlDataReader)cmd.ExecuteReader();
            gvMarkDetails.DataSource = dr3;
            gvMarkDetails.DataBind();
            dr3.Close();
        }
    }

    private void deleteTable(string tableName)
    {
        IDbConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["SurveyConnectionString"].ConnectionString);
        string sql_script = @"DELETE FROM " + tableName + ";";
        using (conn)
        {
            conn.Open();
            IDbCommand cmd = conn.CreateCommand();
            cmd.CommandText = sql_script;
            cmd.ExecuteNonQuery();
        }
    }

    protected void btDownloadData_Click(object sender, EventArgs e)
    {
        //deleteTable("Participants");
        IDataObject dbObj = new Answers();
        Object[] objs = dbObj.enquireDataFromDb();
            
        StreamWriter sw = new StreamWriter(dbObj.FullPath);
        
        foreach (Object obj in objs)
        {            
            sw.WriteLine(obj);
        }
        sw.Close();
        sw.Dispose();
        Response.Clear();
        FileInfo fileInfo = new FileInfo(dbObj.FullPath);
        DateTime today = System.DateTime.Today.Date;
        Response.AddHeader("Content-Disposition", "attachment; filename=" + today.Year + "_" + today.Month + "_" + today.Day + ".txt");
        Response.AddHeader("Content-Length", fileInfo.Length.ToString());
        Response.ContentType = "application/octet-stream";
        Response.TransmitFile(dbObj.FullPath);   
    }

    private bool uploadFile(IDataObject dataObject, FileUpload fileUpload)
    {
        if (dataObject != null)
        {
            if (File.Exists(dataObject.FullPath))
            {
                File.GetAccessControl(dataObject.FullPath);
                File.Delete(dataObject.FullPath);
            }
            fileUpload.PostedFile.SaveAs(dataObject.FullPath);
            dataObject.InsertData();
            return true;
        }
        return false;
    }
}
